package com.ssy.lingxi.search.controller.feign;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.search.api.model.request.CommoditySearchRequest;
import com.ssy.lingxi.search.api.model.request.StoreIdListRequest;
import com.ssy.lingxi.search.api.model.request.TemplateCommoditySearchRequest;
import com.ssy.lingxi.search.api.model.response.BrandResponse;
import com.ssy.lingxi.search.api.model.response.CategoryResponse;
import com.ssy.lingxi.search.api.model.response.CommoditySearchResponse;
import com.ssy.lingxi.search.service.ICommodityService;
import com.ssy.lingxi.search.service.ITemplateService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 模板服务类--商品
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/8/1
 */
@RestController
@RequestMapping("/search/commodity/template")
public class TemplateController extends BaseController {

    @Resource
    private ICommodityService commodityService;

    @Resource
    private ITemplateService templateCommodityService;

    /**
     * 查询商品分类树
     * @param shopId            商城id
     * @param memberId          会员id
     * @param memberRoleId      会员角色id
     */
    @RequestMapping(value = "/getCategoryTree", method = RequestMethod.GET)
    public Wrapper<List<Node>> getCategoryTree(
            @RequestParam(value = "shopId") Long shopId,
            @RequestParam(value = "memberId", required = false) Long memberId,
            @RequestParam(value = "memberRoleId", required = false) Long memberRoleId) {
        return Wrapper.success(templateCommodityService.getCategoryTree(shopId, memberId, memberRoleId));
    }

    /**
     * 通过品类查询会员信息
     * @param shopId            商城id
     * @param categoryId        品类id
     */
    @RequestMapping(value = "/getMemberIdByCategory", method = RequestMethod.GET)
    public Wrapper<List<Long>> getMemberIdByCategory(
            @RequestParam(value = "shopId") Long shopId,
            @RequestParam(value = "categoryId") Long categoryId){
        return Wrapper.success(templateCommodityService.getMemberIdByCategory(shopId, categoryId));
    }

    /**
     * 查询一级品类信息
     * @param shopId            商城id
     * @param memberId          会员id
     * @param memberRoleId      会员角色id
     */
    @RequestMapping(value = "/getFirstCategoryListByMemberId", method = RequestMethod.GET)
    public Wrapper<List<CategoryResponse>> getFirstCategoryList(
            @RequestParam(value = "shopId") Long shopId,
            @RequestParam(value = "memberId", required = false) Long memberId,
            @RequestParam(value = "memberRoleId", required = false) Long memberRoleId){
        return Wrapper.success(templateCommodityService.getFirstCategoryList(shopId, memberId, memberRoleId));
    }

    /**
     * 查询二级品类信息
     * @param shopId            商城id
     * @param memberId          会员id
     * @param memberRoleId      会员角色id
     * @param categoryId        品类id
     */
    @RequestMapping(value = "/getSecondCategoryListByMemberId", method = RequestMethod.GET)
    public Wrapper<List<CategoryResponse>> getSecondCategoryList(
            @RequestParam(value = "shopId") Long shopId,
            @RequestParam(value = "memberId", required = false) Long memberId,
            @RequestParam(value = "memberRoleId", required = false) Long memberRoleId,
            @RequestParam(value = "categoryId") Long categoryId){
        return Wrapper.success(templateCommodityService.getSecondCategoryList(shopId, memberId, memberRoleId, categoryId));
    }

    /**
     * 查询品牌信息
     * @param current 当前页码
     * @param pageSize 每页条数
     * @param customerCategoryId 会员分类id
     * @param categoryId 平台分类id
     * @param memberId 会员id
     * @param name 品牌名称
     * @param idInList 包含
     * @param idNotInList 不包含
     */
    @RequestMapping(value = "/getBrandList", method = RequestMethod.GET)
    public Wrapper<PageData<BrandResponse>> getBrandList(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize,
                                                         @RequestParam(value = "shopId", required = false) Long shopId,
                                                         @RequestParam(value = "customerCategoryId",required = false) Long customerCategoryId,
                                                         @RequestParam(value = "categoryId",required = false) Long categoryId,
                                                         @RequestParam(value = "memberId",required = false) Long memberId,
                                                         @RequestParam(value = "memberRoleId",required = false) Long memberRoleId,
                                                         @RequestParam(value = "memberName",required = false) String memberName,
                                                         @RequestParam(value = "name",required = false) String name,
                                                         @RequestParam(value = "idInList",required = false) List<Long> idInList,
                                                         @RequestParam(value = "idNotInList",required = false) List<Long> idNotInList){
        //构造查询条件
        PageVO pageVO = new PageVO();
        pageVO.setCurrent(current);
        pageVO.setPageSize(pageSize);
        CommoditySearchRequest commoditySearchRequest = new CommoditySearchRequest();
        commoditySearchRequest.setCategoryId(categoryId);
        commoditySearchRequest.setCustomerCategoryId(customerCategoryId);
        commoditySearchRequest.setName(name);
        commoditySearchRequest.setMemberId(memberId);
        commoditySearchRequest.setMemberRoleId(memberRoleId);
        commoditySearchRequest.setMemberName(memberName);
        commoditySearchRequest.setIdInList(idInList);
        commoditySearchRequest.setIdNotInList(idNotInList);
        commoditySearchRequest.setShopId(shopId);

        return Wrapper.success(templateCommodityService.searchBrandList(pageVO, commoditySearchRequest));
    }

    /**
     * 查询商品信息
     * @param templateCommoditySearchRequest 请求参数
     */
    @RequestMapping(value = "/searchCommodityList", method = RequestMethod.POST)
    public Wrapper<PageData<CommoditySearchResponse>> searchCommodityList(@RequestBody TemplateCommoditySearchRequest templateCommoditySearchRequest) {
        //构造查询条件
        CommoditySearchRequest commoditySearchRequest = BeanUtil.copyProperties(templateCommoditySearchRequest, CommoditySearchRequest.class);
        return Wrapper.success(commodityService.searchCommodityList(commoditySearchRequest, false, false));
    }

    /**
     * 根据店铺id集合查找最新上架的商品
     * @param storeIdListRequest 参数
     */
    @RequestMapping(value = "/getCommodityListByPublishTime", method = RequestMethod.POST)
    public Wrapper<Map<Long, List<CommoditySearchResponse>>> getCommodityListByPublishTime(@RequestBody StoreIdListRequest storeIdListRequest){
        return Wrapper.success(templateCommodityService.getCommodityListByPublishTime(storeIdListRequest));
    }

    /**
     * 根据店铺id集合查找销量最高的商品
     * @param storeIdListRequest 参数
     */
    @RequestMapping(value = "/getCommodityListBySoldCount", method = RequestMethod.POST)
    public Wrapper<Map<Long, List<CommoditySearchResponse>>> getCommodityListBySoldCount(@RequestBody StoreIdListRequest storeIdListRequest){
        return Wrapper.success(templateCommodityService.getCommodityListBySoldCount(storeIdListRequest));
    }
}
